<script setup name="Order">
import {orderGetList, orderGetTypeList} from "@/api/wms/order";
import {warehouseGetList} from "@/api/wms/warehouse";
import OrderCurd from "@/views/wms/order/curd";
import {getCurrentInstance, onMounted, reactive} from "vue";
import {useRoute ,useRouter} from "vue-router";
import {stockEditForOrderExec} from "@/api/wms/stock";
import {accountEditForBillExec} from "@/api/fms/account";
import * as path from "path";
const {proxy} = getCurrentInstance();

const {wms_order_state} = proxy.useDict("wms_order_state")
const route = useRoute();
const router = useRouter();
onMounted(() => {
  pd.queryParams.typeId = route.query.typeId;
  orderGetTypeList(pd.queryParams).then(res => {
    // 直接获取第一个省的在多写一个后端
    pd.orderType = res.data[0];
    getList();
  });
  warehouseGetList({parentId: 0}).then(res => {
    pd.warehouseList = res.data;
  });
});

const pd = reactive({
  orderType: {
    stockType: null, fmsType: null, loginName: null, costMoney: null, money: null,
    otherWhName: null, selfWhName: null, isCustomer: null, isSupplier: null, isStock: null
  },
  warehouseList: [],
  orderList: [],
  loading: true,
  showSearch: true,
  total: 0,
  daterangeUpdate_time: [],
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    typeId: null,
    userId: null,
    enableFlag: "0"
  }
});

/** 查询合作伙伴列表 */
function getList() {
  pd.loading = true;
  if (null != pd.daterangeUpdate_time && '' != pd.daterangeUpdate_time) {
    pd.queryParams.beginTime = pd.daterangeUpdate_time[0];
    pd.queryParams.endTime = pd.daterangeUpdate_time[1];
  }
  orderGetList(pd.queryParams).then(res => {
    pd.orderList = res.data;
    pd.total = res.total;
    pd.loading = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  pd.queryParams.beginTime = null;
  pd.queryParams.endTime = null;
  pd.queryParams.name = null;
  pd.daterangeUpdate_time = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  let order = {};
  order.typeId = pd.orderType.typeId;
  order.isStock = pd.orderType.isStock;
  proxy.$refs["orderCurdRef"].add(order);
}

/** 修改按钮操作 */
function handleEdit(order) {
  order.isStock = pd.orderType.isStock;
  proxy.$refs["orderCurdRef"].edit(order);
}

/** 修改按钮操作 */
function handleDel(id) {
  proxy.$refs["orderCurdRef"].del(id);
}

/** 对话框完成后刷新界面 */
function orderCurdOk(operation) {
  getList();
}
/** 跳转到审批页面   */
function redirectToApproval() {

  router.push('/sys/tool/workflow')
}

function orderExec(id){
  proxy.$modal.confirm('是否确认执行单据编号为:"' + id + '"的数据项？').then(function () {
    return stockEditForOrderExec({id: id})
  }).then(res => {
    proxy.$modal.msgSuccess(res.msg);
    getList();
  }).catch(() => {
  });
}
</script>

<template>
  <div class="app-container">
    <el-form :model="pd.queryParams" ref="queryRef" :inline="true" v-show="pd.showSearch" label-width="68px">
      <el-form-item prop="warehouseId">
        <el-select v-model="pd.queryParams.warehouseId" placeholder="请选择仓库" clearable>
          <el-option v-for="item in pd.warehouseList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item :label="pd.orderType.userId" prop="userId">
        <el-input v-model="pd.queryParams.userId" placeholder="请输入搜索用户id" @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="pd.queryParams.name" placeholder="请输入搜索订单名称" @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item label="更新时间" style="width: 308px">
        <el-date-picker v-model="pd.daterangeUpdate_time" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
                        start-placeholder="开始日期" end-placeholder="结束日期"/>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="Search" @click="handleQuery">搜 索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['wms:order:add']">新 增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="Upload" v-hasPermi="['wms:order:add']">导 入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" v-hasPermi="['wms:order:export']">导 出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="pd.showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="pd.loading" :data="pd.orderList">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="订单ID" align="center" prop="id"/>
      <el-table-column label="订单名称" align="center" prop="name">
        <template #default="scope">
          <el-link type="primary" @click="handleEdit(scope.row)" v-hasPermi="['wms:order:edit']">
            {{ scope.row.name }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="单据类型" align="center" prop="typeName"/>
      <el-table-column :label="pd.orderType.userName" align="center" prop="userName"/>
      <el-table-column v-if="pd.orderType.selfWhName != 'hide'" :label="pd.orderType.selfWhName" align="center"
                       prop="selfWhName" width="100"/>
      <el-table-column v-if="pd.orderType.otherWhName != 'hide'" :label="pd.orderType.otherWhName" align="center"
                       prop="otherWhName"/>
      <el-table-column label="单据状态" align="center" prop="state">
        <template #default="scope">
          <dict-tag :options="wms_order_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="总金额" align="center" prop="totalMoney"/>
      <el-table-column label="优惠金额" align="center" prop="discountMoney"/>
      <el-table-column :label="pd.orderType.costMoney" align="center" prop="costMoney"/>
      <el-table-column v-if="pd.orderType.stockType != 'nothing'" label="单据来源" align="center" prop="sourceId"/>
      <el-table-column v-if="pd.orderType.stockType != 'nothing'" label="生产厂商" align="center" prop="manufacturerId"/>
      <el-table-column label="申请人" align="center" prop="userApplicantId"/>
      <el-table-column v-if="pd.orderType.stockType != 'nothing'" label="执行人" align="center" prop="userExecId"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="220">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleEdit(scope.row)" v-hasPermi="['wms:order:edit']">
            修改
          </el-button>
          <el-button link type="primary" icon="DataLine" @click="redirectToApproval()" v-hasPermi="['wms:order:edit']">
            审批
          </el-button>
          <el-button link type="primary" icon="More" @click="orderExec(scope.row.id)" v-hasPermi="['wms:stock:edit']">
            执行
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDel(scope.row.id)"
                     v-hasPermi="['wms:order:del']"> 删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="pd.total > 0" :total="pd.total" v-model:page="pd.queryParams.pageNum"
                v-model:limit="pd.queryParams.pageSize" @pagination="getList"/>
    <OrderCurd ref="orderCurdRef" @ok="orderCurdOk"/>
  </div>
</template>

<style lang='scss' scoped>

</style>